//package com.smxy.hunzi.user.config;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.smxy.hunzi.user.entity.UserEntity;
//import com.smxy.hunzi.user.service.UserService;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.subject.Subject;
//import org.apache.shiro.util.ByteSource;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.util.List;
//
///**
// * @author 李涛
// * @date 2020-08-15 13:32
// */
//public class UserRealm extends AuthorizingRealm {
//    @Autowired
//    private UserService userService;
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//       // System.out.println("执行了=>授权doGetAuthorizationInfo");
//
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//
//
//        //获取当前登录的对象
//        Subject subject = SecurityUtils.getSubject();
//
//        UserEntity currentUser = (UserEntity) subject.getPrincipal();
//        if(currentUser.getPerms() == 1){
//            info.addStringPermission("user:admin");//设置用户管理员权限
//            return info;
//        }
//        return null;
//    }
//
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        //System.out.println(">>>>>>>>>>认证");
//        //从token中取到用户名再去查用户密码
//
//        UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;
//        List<UserEntity> entities = userService.list(new QueryWrapper<UserEntity>()
//                .eq("email", userToken.getUsername()));
//        if(entities == null || entities.size() == 0)
//            return null;
//        //获取当前用户
//        Subject currentSubject = SecurityUtils.getSubject();
//        Session session = currentSubject.getSession();
//        session.setAttribute("loginUser",entities.get(0));
//
//        return new SimpleAuthenticationInfo(entities.get(0),entities.get(0).getPassword(),"");
//    }
//}
